package com.mycompany.app.common.extjs.json;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * json-lib-2.2可以为对应的class注册解析类，记住一定要2.2，2.1有同步问题。切记切记。 
 写一个DateJsonValueProcessor.java 
 * @author Shawn.guo
 *
 */
public class DateJsonValueProcessor implements JsonValueProcessor {
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

    private DateFormat dateFormat;

    public DateJsonValueProcessor() {

        dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
    }

    public DateJsonValueProcessor(String datePattern) {

        try {
            dateFormat = new SimpleDateFormat(datePattern);
        } catch (Exception ex) {
            dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        }
    }

    public Object processArrayValue(Object value, JsonConfig jsonConfig) {

        return process(value);
    }

    public Object processObjectValue(String key, Object value,
            JsonConfig jsonConfig) {

        return process(value);
    }

    private Object process(Object value) {

        return dateFormat.format((Date) value);
    }
}
